package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.ISellingPriceLogService;
import com.wondertek.poms.dao.po.*;
import com.wondertek.poms.dao.productrepo.IProductDao;
import com.wondertek.poms.dao.repo.IContentDao;
import com.wondertek.poms.dao.repo.IContentFeeDao;
import com.wondertek.poms.dao.repo.ISellingPriceLogDao;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p><b>PcProductRuleServiceImpl Description:</b> 售价日志 service层实现 </p>
 *
 * @author 窦再兴
 * <p><b>DATE</b> 2020-10-16 15:56:15</p>
 */
@Slf4j
@Service
@Scope("prototype")
public class SellingPriceLogServiceImpl extends ServiceImpl<SellingPriceLog, Long> implements ISellingPriceLogService {

	@Autowired
	private ISellingPriceLogDao sellingPriceLogDao;
	@Autowired
	private IContentDao contentDao;
	@Autowired
	private IProductDao productDao;
	@Autowired
	private IContentFeeDao contentFeeDao;

	@Override
	public int saveByPragma(ContentFee contentFee, PrdCont prdCont, Content content, Product product,
							String prdInfoId, Long contentFeeId,String optType,String isSal) {
		int count=0;
		try {
			String contentId =prdCont.getContentId();
			if(content==null){
				content=contentDao.findByContentId(contentId);
				if(content==null){
					content=new Content();
				}
			}
			if(StringUtils.isBlank(prdInfoId)){
			 prdInfoId = prdCont.getPrdInfoId().toString();
			}
			if(contentFee==null){
				contentFee=contentFeeDao.getOne(contentFeeId);
			}
			String productId = contentFee.getSaleProductId();
			String start = contentFee.getStartTime();
			String end = contentFee.getEndTime();
			List<Product> productList=null;
			if(product==null) {
				productList = productDao.findInProductIDS(prdCont.getProductId());
			}
			SellingPriceLog sellingPriceLog=new SellingPriceLog();
			if(optType!=null){
				sellingPriceLog.setOptType(optType);
			}
			if(isSal!=null){
				sellingPriceLog.setIsSal(isSal);
			}
			SellingPriceLog.putTimeAndUser(sellingPriceLog);
			sellingPriceLog.setName(content.getName());
			sellingPriceLog.setContentId(contentId);
			sellingPriceLog.setPrice(product.getPrice());
			sellingPriceLog.setPrdInfoId(prdInfoId);
			sellingPriceLog.setProductName(product.getName());
			sellingPriceLog.setProductId(productId);
			sellingPriceLog.setSalStartTime(start);
			sellingPriceLog.setSalEndTime(end);
			if(productList!=null && productList.size()>0){
				for (Product product0:productList) {
					sellingPriceLog.setProductName(product0.getName());
					sellingPriceLog.setPrice(product0.getPrice());
					sellingPriceLogDao.save(sellingPriceLog);
					count++;
				}
			}else {
				if(product!=null) {
					sellingPriceLog.setProductName(product.getName());
					sellingPriceLog.setPrice(product.getPrice());
				}
				sellingPriceLogDao.save(sellingPriceLog);
			}
			log.error("SellingPriceLogImpl#saveBy4Class 保存日志失败");
		}catch (Exception e){

			return 0;
		}
		return 1;
	}

	@Override
	public int saveByProductList(ContentFee contentFee, PrdCont prdCont, Content content, List<Product> productList, String prdInfoId, Long contentFeeId, String optType, String isSal) {
		int count=0;
		try {
			String contentId =prdCont.getContentId();
			if(content==null){
				content=contentDao.findByContentId(contentId);
				if(content==null){
					content=new Content();
				}
			}
			if(StringUtils.isBlank(prdInfoId)){
				prdInfoId = prdCont.getPrdInfoId().toString();
			}
			if(contentFee==null){
				contentFee=contentFeeDao.getOne(contentFeeId);
			}
			String productId = contentFee.getSaleProductId();
			String start = contentFee.getStartTime();
			String end = contentFee.getEndTime();
			SellingPriceLog sellingPriceLog=new SellingPriceLog();
			if(optType!=null){
				sellingPriceLog.setOptType(optType);
			}
			if(isSal!=null){
				sellingPriceLog.setIsSal(isSal);
			}
			SellingPriceLog.putTimeAndUser(sellingPriceLog);
			sellingPriceLog.setName(content.getName());
			sellingPriceLog.setContentId(contentId);
			sellingPriceLog.setPrdInfoId(prdInfoId);
			sellingPriceLog.setProductId(productId);
			sellingPriceLog.setSalStartTime(start);
			sellingPriceLog.setSalEndTime(end);
			for (Product product:productList) {
				sellingPriceLog.setProductName(product.getName());
				sellingPriceLog.setPrice(product.getPrice());
				sellingPriceLogDao.save(sellingPriceLog);
				count++;
			}
			log.error("SellingPriceLogImpl#saveBy4Class 保存日志失败");
		}catch (Exception e){
			log.error("SellingPriceLogImpl#saveBy4Class 保存日志失败");
			return count;
		}
		return count;
	}

	@Override
	public SellingPriceLog deleteLogic(Long id) {
		 sellingPriceLogDao.deleteById(id);
		 return null;
	}

	@Override
	public SellingPriceLog merge(SellingPriceLog model) {
		return null;
	}


}